package com.zhou.utils.easyExcel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.cache.MapCache;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class ExcelHelper {
    /**
     * 读取Excel  @ExcelProperty(列表名称)、@ExcelIgnore（忽略这一行）
     * @param file
     * @param head
     * @param sheetNames
     * @return
     * @param <T>
     */
    //导入
    public static <T> List<T> readExcel(String file, Class<T> head, String... sheetNames) {
        List<T> list = new ArrayList<>();
        for (String sheetName : sheetNames) {
            list.addAll(readExcel(file, head, sheetName));
        }
        return list;
    }
    //导入
    public static <T> List<T> readExcel(String file, Class<T> head, String sheetName) {
        ExcelListener<T> listener = new ExcelListener<>();
        EasyExcel.read(file, head, listener).readCache(new MapCache()).sheet(sheetName).doRead();
        return listener.getData();
    }
    //导入
    public static <T> List<T> readExcel(String file, Class<T> head, int sheet) {
        ExcelListener<T> listener = new ExcelListener<>();
        EasyExcel.read(file, head, listener).readCache(new MapCache()).sheet(sheet).doRead();
        return listener.getData();
    }
    //导入
    public static <T> List<T> readExcel(String file, Class<T> head, int sheet, int headRowNumber) {
        ExcelListener<T> listener = new ExcelListener<>();
        EasyExcel.read(file, head, listener).readCache(new MapCache()).sheet(sheet).headRowNumber(headRowNumber).doRead();
        return listener.getData();
    }
 
    //导出
    public static <T> void writeExcel(String file, List<List<String>> head, String sheetName, List<T> list) {
        EasyExcel.write(file).sheet(sheetName).head(head).doWrite(list);
    }
    //导出
    @SuppressWarnings("rawtypes")
    public static <T> void writeExcel(String file, Class head, String sheetName, List<T> list) {
        log.info("正在准备导出数据");
        EasyExcel.write(file).sheet(sheetName).head(head).doWrite(list);
        log.info("导出数据结束");
    }


}